/**
 * Created by XianTao on 2016/11/14
 * <p/>
 * Copyright (c) 2015-2015
 * Apache License
 * Version 2.0, January 2004
 * http://www.apache.org/licenses/
 */
package com.aspire.portal.web.security.client;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.X509EncodedKeySpec;

public class VerifySignature {
    private static final char[] bcdLookup = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    public VerifySignature() {
    }

    public static final String bytesToHexStr(byte[] bcd) {
        StringBuffer s = new StringBuffer(bcd.length * 2);

        for (int i = 0; i < bcd.length; ++i) {
            s.append(bcdLookup[bcd[i] >>> 4 & 15]);
            s.append(bcdLookup[bcd[i] & 15]);
        }

        return s.toString();
    }

    public static final byte[] hexStrToBytes(String s) {
        byte[] bytes = new byte[s.length() / 2];

        for (int i = 0; i < bytes.length; ++i) {
            bytes[i] = (byte) Integer.parseInt(s.substring(2 * i, 2 * i + 2), 16);
        }

        return bytes;
    }

    public static void main(String[] args) {
        GenerateSignature a = new GenerateSignature();
        System.out.println(a.sign("Msisdn=13475417474&CompanyCode=TX&ProductCode=txyx&TransactionID=2014050500000011&Credit=10.99&OperType=0&DateTime=20140505111111", "D:\\cc_project\\wapyx\\Config\\key\\LLH0001_YXLM\\private.key"));
    }

    public boolean verify(String info, String sign, String path) {
        File file = new File(path);
        if (file.exists() && file.isFile()) {
            FileInputStream fis = null;

            try {
                fis = new FileInputStream(file);
                byte[] e = new byte[1024];
                StringBuffer s = new StringBuffer();

                int size;
                while ((size = fis.read(e)) != -1) {
                    s.append(new String(e, 0, size));
                }

                X509EncodedKeySpec bobPubKeySpec = new X509EncodedKeySpec(hexStrToBytes(s.toString()));
                KeyFactory keyFactory = KeyFactory.getInstance("RSA");
                PublicKey pubKey = keyFactory.generatePublic(bobPubKeySpec);
                byte[] signed = hexStrToBytes(sign);
                Signature signetcheck = Signature.getInstance("MD5withRSA");
                signetcheck.initVerify(pubKey);
                signetcheck.update(info.getBytes("ISO-8859-1"));
                if (!signetcheck.verify(signed)) {
                    return false;
                }
            } catch (Exception var23) {
                var23.printStackTrace();
                return false;
            } finally {
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException var22) {
                        var22.printStackTrace();
                    }
                }

            }

            return true;
        } else {
            return false;
        }
    }
}
